Page({
  data: {
    id: '...',
    cutColor: '#cbcbcb88',
    guangImg1: '../../images/click_before.png',
    guangImg: '../../images/click_after.png',
    // url: '/images/click_before.png',
    isShow1: '',
    m: '',
    person: [],
    chooseUserid: '',
    sessionid: null, //登录判断
    gTags: 0,
    amount: '...',
    bgImage: '../../images/bg.png',
    rankBg: '../../images/rank_bg.png',
  },

  paiHang: function () {
    //排行点赞
    var that = this
    wx.request({
      url: getApp().globalData.ip + '/ranking/list',
      method: 'GET',
      header: {
        'content-type': 'application/json;charest=utf-8',
      },
      success: function (res) {
        console.log('光币排行', res)
        that.setData({
          person: res.data,
        })
        let person = that.data.person
        for (let i in person) {
          let dakaPerson = wx.getStorageSync('dakaPerson')
          let DataUserId = person[i].userId
          for (let j = 0; j < dakaPerson.length; j++) {
            let StorUserId = dakaPerson[j].userId
            if (StorUserId == DataUserId) {
              person[i].isShow = dakaPerson[j].isShow
            }
          }
          if (person[i].isShow === false) {
            person[i].isShow = false
          }
        }
        // that.setData({
        //   person: person.slice(0, 30),
        // })
      },
    })
  },

  change(e) {
    //自我点赞
    let that = this
    var index = e.currentTarget.dataset.index
    let zanInfo = getApp().globalData.ip + '/ranking/gtags'
    let person = this.data.person
    var chooseUserid = this.data.person[index].userId
    let gTags = that.data.gTags
    if (!person[index].isShow) {
      person[index].gtags++
      person[index].isShow = true
      that.setData({
        person,
      })
      if (that.data.m === e.currentTarget.dataset.userid) {
        that.setData({
          gTags: gTags + 1,
        })
      }
      wx.setStorageSync('dakaPerson', person)
      var uid = chooseUserid
      wx.request({
        url: `${zanInfo}/${uid}`,
        data: {},
        header: {
          cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
        },
        method: 'PUT',
        type: 'json',
        success(res) {
          console.log('点赞接口: ', res)
        },
      })
    }
  },

  myPaiHang() {
    //返回我的排名和次数
    var that = this
    var r = getApp().globalData.ip + '/ranking/clock'
    var uid = that.data.m
    wx.request({
      url: `${r}/${uid}`,
      header: {
        cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
      },
      method: 'GET',
      type: 'json',
      success(r_s) {
        console.log('我的排名:', r_s)
        that.setData({
          id: r_s.data.id,
          amount: r_s.data.amount,
        })
      },
    })
  },

  onLoad: function (option) {
    var that = this
    that.setData({
      sessionid: getApp().globalData.sessionid,
    })
    that.paiHang()
    wx.request({
      url: getApp().globalData.ip + '/account/detail',
      data: {},
      header: {
        cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        console.log('detail:', res)
        that.setData({
          gTags: res.data.data.gTags,
          m: res.data.data.userId, //我的userId
        })
        that.myPaiHang() //异步
      },
    })

    //缓存
    let newTime = Date.parse(new Date())
    let oldTime = wx.getStorageSync('oldTime')
    if (oldTime && oldTime > newTime) {
      let dakaPerson = wx.getStorageSync('dakaPerson')
      that.setData({
        person: dakaPerson,
      })
    } else {
      let oldTime = newTime + 86400000
      wx.setStorageSync('oldTime', oldTime)
      wx.removeStorageSync('dakaPerson')
      that.paiHang()
    }
  },

  onPullDownRefresh: function () {
    this.onLoad()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },
})
